package com.daigoudao.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.daigoudao.util.Constants;
import com.daigoudao.web.dao.IShortMessageDao;
import com.daigoudao.web.model.ShortMessage;
import com.daigoudao.web.service.IShortMessageService;

public class ShortMessageServiceImpl implements IShortMessageService {

	private IShortMessageDao shortMessageDao;

	public void setShortMessageDao(IShortMessageDao shortMessageDao) {
		this.shortMessageDao = shortMessageDao;
	}

	public void addShortMessage(ShortMessage shortMessage) throws Exception {
		shortMessageDao.addShortMessage(shortMessage);
	}

	public void addShortMessages(List<ShortMessage> shortMessage)
			throws Exception {
		shortMessageDao.addShortMessages(shortMessage);
	}

	public List<ShortMessage> queryShortMessage(ShortMessage shortMessage)
			throws Exception {
		return shortMessageDao.queryShortMessages(shortMessage);
	}

	public int getMessageCount(ShortMessage shortMessage) throws Exception {
		return shortMessageDao.getMessageCount(shortMessage);
	}

	@Override
	public boolean saveShortMessage(ShortMessage shortMessage) throws Exception {
		return shortMessageDao.saveShortMessage(shortMessage) == 1 ? true
				: false;
	}

	public List<ShortMessage> queryReplys(int smid) throws Exception {
		List<ShortMessage> replyMessages = new ArrayList<ShortMessage>();
		// 查询出当前消息
		ShortMessage shortMessage = null;
		int replyId = smid;
		while (true) {
			shortMessage = shortMessageDao.queryReplyMessage(replyId);
			if (shortMessage != null) {
				replyMessages.add(0, shortMessage);
				replyId = shortMessage.getSmid();
			} else {
				break;
			}
		}
		return replyMessages;
	}

	public void delMessages(int userType, int[] smids) throws Exception {
		if (Constants.USER_TYPE_BROKER == userType) {
			for (int smid : smids) {
				shortMessageDao.delMessageByBroker(smid);
			}
		} else {
			for (int smid : smids) {
				shortMessageDao.delMessageByBuyer(smid);
			}
		}
	}

	public List<ShortMessage> queryShortMessagesByBroker(
			ShortMessage shortMessage) throws Exception {
		List<ShortMessage> shortMessages = shortMessageDao
				.queryShortMessagesByBroker(shortMessage);
		for (ShortMessage message : shortMessages) {
			List<ShortMessage> replyMessages = queryReplys(message.getSmid());
			message.setReplyMessages(replyMessages);
			ShortMessage replyMessage = new ShortMessage();
			replyMessage.setSmid(message.getSmid());
			replyMessage.setContent(message.getContent());
			replyMessage.setSender(message.getSender());
			replyMessage.setCreatetime(message.getCreatetime());
			replyMessage.setSenderName(message.getSenderName());
			replyMessages.add(replyMessage);
			for (ShortMessage tmp : replyMessages) {
				if (tmp.getReceiver() == message.getReceiver()) {
					message.setContent(tmp.getContent());
					message.setCreatetime(message.getCreatetime());
					break;
				}
			}
		}

		return shortMessages;
	}

	@Override
	public int getShortMessageCountByBroker(ShortMessage shortMessage)
			throws Exception {
		return shortMessageDao.getShortMessageCountByBroker(shortMessage);
	}

	@Override
	public void addReplyMessage(ShortMessage shortMessage) throws Exception {
		shortMessageDao.addShortMessage(shortMessage);
		// 更改主消息id的状态
		ShortMessage mainShortMessage = new ShortMessage();
		mainShortMessage.setSmid(shortMessage.getMainSmid());
		mainShortMessage.setReplyTag(shortMessage.getReplyTag());
		mainShortMessage.setRead(1);
		mainShortMessage.setUpdateTime(shortMessage.getCreatetime());
		shortMessageDao.updateMessageStatus(mainShortMessage);

	}

	public void alreadyRead(int smid) throws Exception {
		shortMessageDao.alreadyRead(smid);
	}

	public List<ShortMessage> queryShortMessagesByBuyer(
			ShortMessage shortMessage) throws Exception {
		List<ShortMessage> shortMessages = shortMessageDao
				.queryShortMessagesByBuyer(shortMessage);
		for (ShortMessage message : shortMessages) {
			List<ShortMessage> replyMessages = queryReplys(message.getSmid());
			message.setReplyMessages(replyMessages);
			ShortMessage replyMessage = new ShortMessage();
			replyMessage.setSmid(message.getSmid());
			replyMessage.setContent(message.getContent());
			replyMessage.setSender(message.getSender());
			replyMessage.setCreatetime(message.getCreatetime());
			replyMessage.setSenderName(message.getSenderName());
			replyMessages.add(replyMessage);

			for (ShortMessage tmp : replyMessages) {
				if (tmp.getSender() != message.getSender()) {
					message.setContent(tmp.getContent());
					message.setCreatetime(message.getCreatetime());
					message.setSenderName(tmp.getSenderName());
					break;
				}
			}
		}
		return shortMessages;
	}

	public int getShortMessageCountByBuyer(ShortMessage shortMessage)
			throws Exception {
		return shortMessageDao.getShortMessageCountByBuyer(shortMessage);
	}
}
